﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class testing : System.Web.UI.Page
{
    private static string CODELOGIN = "", PASSWORD = "", DATETEST = "", TIMETEST = "", ROUND = "";
    private SQLQuery sqlQuery = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["candidate"] == null)
        {
            Response.Redirect("index.aspx");
        }
        loadUserControls();
        sqlQuery = new SQLQuery();
        if (!IsPostBack)
        {
            if (sqlQuery.searchInforExamTest(mGridView, Session["candidate"].ToString()))
            {
                lbl_round_list.Text = "";
                layout_login.Visible = true;
            }
            else
            {
                lbl_round_list.Text = LocalVariables.WAITING_FOR_CHECKS;
                layout_login.Visible = false;
            }
        }
    }

    private void loadUserControls()
    {
        string path = "panel/menulist.ascx";
        panel_menulist.Controls.Clear();
        UserControl userControl = (UserControl)LoadControl(path);
        panel_menulist.Controls.Add(userControl);
    }

    protected void mGridView_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbl_msg.Text = "";
        string status = mGridView.SelectedRow.Cells[6].Text.Trim();
        if (status.Equals("waiting"))
        {
            ROUND = mGridView.SelectedRow.Cells[0].Text.Trim();
            CODELOGIN = mGridView.SelectedRow.Cells[2].Text.Trim();
            PASSWORD = mGridView.SelectedRow.Cells[3].Text.Trim();
            DATETEST = mGridView.SelectedRow.Cells[4].Text.Trim();
            TIMETEST = mGridView.SelectedRow.Cells[5].Text.Trim().Replace(":", "");

            lbl_examcode.Text = mGridView.SelectedRow.Cells[1].Text.Trim();
            txt_code.Text = CODELOGIN;
            btn_login.Enabled = true;
        }
        else
        {
            lbl_examcode.Text = "";
            txt_code.Text = "";
            txt_pass.Text = "";
            lbl_msg.Text = "You've " + status + " this round";
        }
    }

    protected void btn_login_Click(object sender, EventArgs e)
    {
        lbl_msg.Text = "";
        string code = txt_code.Text.Trim();
        string pass = txt_pass.Text.Trim();
        if (code.Equals(CODELOGIN) && pass.Equals(PASSWORD))
        {
            if (LocalVariables.getDate().Equals(DATETEST))
            {
                if (LocalVariables.getTime() >= Convert.ToInt32(TIMETEST))
                {
                    if (sqlQuery.updateTested(Session["candidate"].ToString(), ROUND, 0, "", "", "tested"))
                    {
                        LocalVariables.clearValue();
                        sqlQuery.loadContentQuestion(lbl_examcode.Text);
                        Response.Redirect("testonline.aspx?examcode=" + lbl_examcode.Text + "&round=" + ROUND);
                    }
                }
                else
                {
                    lbl_msg.Text = LocalVariables.LESS_TEST_TIME;
                    return;
                }
            }
            else
            {
                lbl_msg.Text = LocalVariables.NOT_DATE_TEST;
                return;
            }            
        }
        else
        {
            lbl_msg.Text = LocalVariables.CONNECT_FAILURE;
        }
    }
}